﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;
using Toto.Telas;
using Toto.Componentes;

namespace Toto.Estado
{

    /// <summary>
    /// Classe auxiliar que representa um único item do Menu.
    /// Por padrão isto só desenha a string de entrada de texto,
    /// mas pode ser personalizada para exibir itens de menu de diferentes maneiras.
    /// Isso também fornece um evento que será disparado quando o item de menu é selecionado.
    /// </summary>
    public class MenuItem
    {
        #region [ Fields ]

        private string text;
        private float selectionFade;
        private SpriteFont font;
        private GameState gameState;
        private float scale;

        #endregion

        #region [ Properties ]

        public GameState SelectedScreen
        {
            get { return gameState; }
        }

        #endregion

        #region [ Constructor ]

        /// <summary>
        /// Constrói um novo item para o menu.
        /// </summary>
        public MenuItem(Game1 game, GameState gameScreen, string text)
        {
            this.text = text;
            this.font = GameFont.menu;
            this.gameState = gameScreen;
            this.scale = 1;
        }

        #endregion

        #region [ Update and Draw ]

        /// <summary>
        /// Updates the menu entry.
        /// </summary>
        public void Update( GameScreen screen, bool isSelected, GameTime gameTime)
        {
            float fadeSpeed = (float)gameTime.ElapsedGameTime.TotalSeconds * 4;

            if (isSelected)
                selectionFade = Math.Min(selectionFade + fadeSpeed, 1);
            else
                selectionFade = Math.Max(selectionFade - fadeSpeed, 0);
        }

        /// <summary>
        /// Draws the menu entry. This can be overridden to customize the appearance.
        /// </summary>
        public void Draw(MenuScreen screen, Vector2 position, bool isSelected, GameTime gameTime)
        {
            // Draw the selected entry in blue, otherwise white.
            Color color = isSelected ? Color.DarkGoldenrod : Color.Blue;

            // Pulsate the size of the selected menu entry.
            double time = gameTime.TotalGameTime.TotalSeconds;

            float pulsate = (float)Math.Sin(time * 6) + 1;

            scale = 1 + pulsate * 0.05f * selectionFade;

            // Modify the alpha to fade text out during transitions.
            color = new Color(color.R, color.G, color.B);

            Vector2 origin = new Vector2(0, font.LineSpacing / 2);

            screen.SpriteBatch.DrawString(font, text, position, color, 0,
                                   origin, scale, SpriteEffects.None, 0);
        }

        /// <summary>
        /// Queries how much space this menu entry requires.
        /// </summary>
        public int GetHeight(MenuScreen screen)
        {
            return font.LineSpacing;
        }

        public Vector2 MeasureString()
        {
            return font.MeasureString(text) * scale;
        }

        #endregion
    }
}